package gui;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import java.awt.Rectangle;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JMenuItem;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import controle.ClienteBroadcastListener;
import controle.ClienteProtocoloListener;
import controle.ClienteProtocoloWorker;
import controle.MensagemCliente;
import controle.MensagemServidor;
import controle.MensagemServidorBroadcast;

import javax.swing.SwingConstants;

import usuario.Usuario;

public class Gui extends JFrame {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JMenu jMenu = null;

	private JMenuBar jJMenuBar = null;

	private JMenuItem jMenuItem = null;
	
	private Icon connectIcon = null;

	private JMenuItem jMenuItem1 = null;

	private JLabel jLabel = null;

	private JTextField jTextField = null;

	private JLabel jLabel1 = null;

	private JButton jButton = null;

	private JPasswordField jPasswordField = null; 
	
	private ClientConnected conexaoCliente = null;
	
	private String nome = "";
	
	private String email = "";
	
	private String senha = "";
	
	private String Ip = null;
	
	private int tipo = 1; 
	
	private MensagemCliente mensagemCliente = null;
	
	private MensagemServidor mensagemServidor = null;

	private InetAddress localaddr = null;  //  @jve:decl-index=0:

	private JLabel jLabel2 = null;
	
	private ClienteProtocoloWorker conexaoClienteServidor = null;
	
	private ClienteProtocoloListener clienteListener = null;  //  @jve:decl-index=0:
	
	private Usuario usuario = null;
	
	private MensagemServidorBroadcast	mensagemBroadcast	= null;
	
	private ClienteBroadcastListener	mensagemClienteBroadcast	=	null;

	private JLabel jLabel3 = null;
	
	/**
	 * This is the default constructor
	 */
	public Gui() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(450, 700);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		mensagemClienteBroadcast = new ClienteBroadcastListener(this); 
		mensagemClienteBroadcast.start();

		
		this.connectIcon = new ImageIcon( getClass().getResource( "/live/Live/imagens/cadeado_fechado.jpg" ) );
		
		this.setJMenuBar(getJJMenuBar());
		this.setContentPane(getJContentPane());
		this.setTitle("LiveVox");
		
		
		this.jJMenuBar.add(this.jMenu);
	}

	public void setClienteGui(MensagemServidor msg) 
	{
		if(this.conexaoCliente == null)
		{
			this.setEnabled(false);
			this.conexaoCliente = new ClientConnected(msg);
			this.conexaoCliente.setResizable(false);
			this.conexaoCliente.setVisible(true);
//			if(!this.conexaoCliente.isShowing())//verificar uma função que indique que o frame foi fechado
//			{
//				//mandar uma msg ao servidor para ser retirado da lista de ON-LINE
//			}
		}		
	}
	public void setClienteGui(MensagemServidorBroadcast mensagemBroadcast) 
	{
		this.mensagemBroadcast = mensagemBroadcast;
		this.conexaoCliente.setListaContatos(mensagemBroadcast);	
	}
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(188, 22, 128, 62));
			jLabel3.setText("");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(111, 216, 204, 24));
			jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
			jLabel2.setHorizontalTextPosition(SwingConstants.CENTER);
			jLabel2.setText("");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(111, 166, 204, 23));
			jLabel1.setText("Senha");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(111, 116, 204, 23));
			jLabel.setText("Email");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(jLabel, null);
			jContentPane.add(getJTextField(), null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(getJButton(), null);
			jContentPane.add(getJPasswordField(), null);
			jContentPane.add(jLabel2, null);
			jContentPane.add(jLabel3, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJMenu() {
		if (jMenu == null) {
			jMenu = new JMenu("Arquivo");
			jMenu.setMnemonic('A');
			jMenu.setBounds(new Rectangle(0, 0, 73, 13));
			jMenu.add(getJMenuItem());
			
		}
		return jMenu;
	}

	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getJMenu());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getJMenuItem() {
		if (jMenuItem == null) {
			jMenuItem = new JMenuItem("Entrar", this.connectIcon);
			jMenuItem.setMnemonic('E');
			jMenuItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					conectar();
				}
			});
		}
		return jMenuItem;
	}

	
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(111, 141, 204, 23));
		}
		return jTextField;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton("Entrar", this.connectIcon);
			jButton.setBounds(new Rectangle(147, 243, 134, 37));
			jButton.setBackground(Color.white);
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					conectar();
				}
			});
		}
		return jButton;
	}

	
	private void conectar() 
	{
		try 
		{
			this.jButton.setEnabled(false);
			this.jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/live/Live/imagens/loading2.gif")));
			this.jLabel3.setVisible(true);
			this.email = this.jTextField.getText();
			this.senha = String.copyValueOf(this.jPasswordField.getPassword());
			this.localaddr = InetAddress.getLocalHost();
			this.Ip = localaddr.getHostAddress();
			
			this.usuario = new Usuario(this.nome, this.email, this.senha , this.Ip);
			
			this.mensagemCliente = new MensagemCliente(this.usuario, this.tipo);
			
			this.clienteListener = new ClienteProtocoloListener(this);
			this.clienteListener.start();
			this.conexaoClienteServidor = new ClienteProtocoloWorker(mensagemCliente);
			this.conexaoClienteServidor.start();
		} 
		catch (UnknownHostException e) 
		{
			e.printStackTrace();
		}
		//System.out.println("email: " + email + " Senha: " + senha + " Nome: " + nome + " Ip: " + Ip + " Tipo: " + tipo)
	}
	
	/**
	 * This method initializes jPasswordField	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setBounds(new Rectangle(111, 191, 204, 23));
		}
		return jPasswordField;
	}

	public void erro()
	{
		this.jTextField.setText("");
		this.jPasswordField.setText("");
		this.jButton.setEnabled(true);
		this.jLabel3.setEnabled(false);
	}
	
}  //  @jve:decl-index=0:visual-constraint="10,11"
